<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class WorkerForm extends CFormModel {

    public $firstname;
    public $lastname;
    public $role_id;
    public $login;
    public $pass_hash;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('firstname,lastname,role_id, login, pass_hash', 'required', 'message' => 'Pole wymagane'),
            array('pass_hash', 'length','on' => 'add', 'min' => 8, 'tooShort' => 'Minimalna ilośc znaków to 8'),
            array('firstname,lastname', 'length', 'min' => 3, 'message' => 'Minimalna ilośc znaków to 3'),
        );
    }
            
    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'firstname' => 'Imię',
            'lastname' => 'Nazwisko',
            'role_id' => 'Rola',
            'login' => 'Login',
            'pass_hash' => 'Hasło',
        );
    }
   
    public function addWorker(){
        return Yii::app()->db->createCommand("SELECT createworkerfun('".$this->firstname."', '".$this->lastname."', '".$this->role_id."', '".$this->login."', '".md5($this->pass_hash)."')")->execute();
    }
    
    public function editWorker($id){
        $result = Yii::app()->db->createCommand("SELECT editworkerfun('".$id."', '".$this->firstname."', '".$this->lastname."', '".$this->role_id."', '".$this->login."', '".md5($this->pass_hash)."')")->queryAll();
        return $result[0]['editworkerfun'];
    }
}
